﻿using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cl.Quartz.Jobs.Test
{
    public class TestJobOperation
    {
        public static string _groupName = string.Empty;
        /// <summary>
        /// 工厂
        /// </summary>
        public static ISchedulerFactory _factory;
        /// <summary>
        /// 调度器
        /// </summary>
        public static IScheduler _scheduler;

        public static int _id;

        public static JobKey _jobKey;

        public static TriggerKey _triggerKey;
        public TestJobOperation(int id)
        {
            _groupName = "group1";
            _factory = new StdSchedulerFactory();
            _scheduler = _factory.GetScheduler().Result;

            _jobKey = new JobKey(_groupName + id + "j", _groupName);
            _triggerKey = new TriggerKey(_groupName + id + "t", _groupName);
        }


        public static void AddQuartzJob()
        {

            DeleteQuartzJob();

            //设置Job  StatefulJob 
            var _job = JobBuilder.Create<TestJob>()
           .WithIdentity(_jobKey)
           .Build();


            //传参

            _job.JobDataMap.Put("FreeTime", DateTime.Now);
            //【方法1】 设置每秒执行
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity(_triggerKey)
                .WithSimpleSchedule(x => x.WithIntervalInSeconds(1)
                .RepeatForever())
                .StartAt(new DateTimeOffset(DateTime.Now))
                .EndAt(new DateTimeOffset(DateTime.Now.AddMinutes(10)))
                .Build();

            _scheduler.ScheduleJob(_job, trigger);
        }

        public static void UpdateQuartzJob()
        {
            var GjobDetail = _scheduler.GetJobDetail(_jobKey);
            if (GjobDetail.Result == null)
                return;


            var triggerr = _scheduler.GetTrigger(_triggerKey);
            var triggerBuilder = triggerr.Result.GetTriggerBuilder();
            //修改结束时间 WithCronSchedule(ExecSs).
            ITrigger newTrigger = triggerBuilder.EndAt(DateTime.Now.AddMinutes(5)).Build();



            var job = GjobDetail.Result;
            //job.JobDataMap.Put("AuctionId", Id);
            //job.JobDataMap.Put("MarkId", Mid);
            //job.JobDataMap.Put("FreeTime", FreeTime);
            //job.JobDataMap.Put("LimitTime", LimitTime);


            //删除任务
            _scheduler.DeleteJob(_jobKey);
            _scheduler.ScheduleJob(job, newTrigger);


            //修改最终结束的定时任务；
            //SetAuctioneds setAuctioneds = new SetAuctioneds();
            //setAuctioneds.SetEndQz(Id, FreeTime, Mid);
        }

        public static void DeleteQuartzJob()
        {
            var GjobDetail = _scheduler.GetJobDetail(_jobKey);
            if (GjobDetail != null)
            {
                //删除任务
                _scheduler.DeleteJob(_jobKey);
            }
        }

        public static string PauseQuartzJob()
        {


            var Result = _scheduler.GetTriggerState(_triggerKey).Result;
            if (Result == TriggerState.None)
            {
                return "失败：不存在此任务！";
            }


            if (Result == TriggerState.Paused)//暂停
            {
                return "失败：该任务已暂停！";

            }
            else
            {
                _scheduler.PauseTrigger(_triggerKey);
                return "成功：任务已暂停";
            }
        }
    }
}
